package com.wwwzh.himalayafm.adapter.category;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.XmlyActivity;
import com.wwwzh.himalayafm.entity.entity_category.CategoryEntity;
import com.wwwzh.himalayafm.fragment.disfragment.CategoryDetailFragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */

public class CategoryRecyclerViewAdpter extends RecyclerView.Adapter<CategoryRecyclerViewAdpter.ViewHolder> {
    private Context context;
    private List<CategoryEntity> list;
    private DrawerLayout drawerLayout;
    private final XmlyActivity activity;

    public CategoryRecyclerViewAdpter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        activity = (XmlyActivity) context;
    }

    @Override
    public int getItemViewType(int position) {

        if (list.get(position).getId() == 33) {

            return R.layout.fragment_category_header;
        } else {

            return R.layout.fragment_category_content;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(viewType, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        CategoryEntity item = list.get(position);
        if (item.getId() == 33) {
            Picasso.with(context).load(item.getCoverPath()).into(holder.iv);
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "暂时无法跳转哦~~~~(>_<)~~~~", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            final String title = item.getTitle();
            if (title != null) {
                holder.tv_Item.setText(item.getTitle());
            }
            if (item.getCoverPath() != null) {
                Picasso.with(context).load(item.getCoverPath()).into(holder.iv_Item);
            }
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager manager = activity.getSupportFragmentManager();
                    CategoryDetailFragment detailFragment = new CategoryDetailFragment();
                    Bundle args = new Bundle();
                    args.putString("title", title);
                    detailFragment.setArguments(args);
                    manager.beginTransaction().replace(R.id.drawer_container, detailFragment).commit();
                    DrawerLayout drawerLayout = activity.getDrawerLayout();
                    drawerLayout.openDrawer(Gravity.RIGHT, true);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(Collection<? extends CategoryEntity> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private ImageView iv_Item;
        private TextView tv_Item;
        private LinearLayout linearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_category_id);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_category_content_id);
            iv_Item = (ImageView) itemView.findViewById(R.id.iv_category_item_id);
            tv_Item = (TextView) itemView.findViewById(R.id.tv_category_item_id);
        }
    }
}
